package agents;

import utils.Dijkstra;
import env.City;
import env.DistList;
import env.Move;
import env.Road;
import env.World;

public class Greedy implements Agent {

	private World world;
	private City goal;
	private int score;
	private boolean done;
	public Greedy(World world, City init, City goal) {
		this.world = world;
		this.goal = goal;
		score = 0;
		done = false;
	}
	public void setCity(City to) {

		
	}
	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	@Override
	public Move calcMove() {
		City currentCity = world.getCityOfAgent(this);
		DistList dist = Dijkstra.runDijkstra(world, currentCity, true);
		int goalDistance = dist.getDistance(goal);
		boolean chemsOutsideGoal = world.chemsOutsideCity(goal);
		if (done || (goalDistance == 0 && !chemsOutsideGoal)) {//agent at goal and all chems in goal we are done agent will nop
			done = true;
			return null;
			
		}
		if (goalDistance == Integer.MAX_VALUE) {// if goal is blocked then no-op
			return null;
		}
		City nearestChem = dist.nearestChem();
		if (nearestChem == null) {// no more reacheable chems then no-op
			return null;
		}
		if (currentCity.getChems() == 0) {
			Road road = dist.getNextRoadInPath(nearestChem); 
			return new Move(this, road, currentCity,
					road.getNeighbor(currentCity), false, false);
		}
		if (currentCity.getChems() > 0) {
			Road road = dist.getNextRoadInPath(goal);
			return new Move(this, road, currentCity,
					road.getNeighbor(currentCity), false, true);

		}
		return null;
	}

	@Override
	public boolean isDone() {
		if(done)
			return done;
		else
		{
			done = !world.chemsOutsideCity(goal);
			return done;
		}
	}
public City getGoal() {
		
		return goal;
	}

}
